<template>
	<view>
		<u-modal :show="show" title="优惠券详情" @confirm="show=false">
			<view class="slot-content" style="height: 60vh;overflow: auto;">
				{{content}}
			</view>
		</u-modal>
		<view class="coupon-list" v-if="couponsList.length > 0">
			<view class="item acea-row row-center-wrapper" v-cloak v-for="(item, index) in couponsList" :key="index"
				style="border-radius: 10rpx;overflow: hidden;">
				<view class="money moneyGray" :class="item._type === 0 ? 'moneyGray' : ''"
					style="background-color: red;">
					<view>
						￥<span class="num">{{item.price}}</span>
					</view>
					<view class="pic-num">{{item.name}}</view>
				</view>
				<view class="text">
					<view class="condition line1">
						{{item.intro}}
					</view>
					<view class="data acea-row row-between-wrapper">
						<view>{{formatTimestamp(item.createtime)}}</view>
						<!-- <view v-else>{{ item.createTime }}-{{ item.endTime }}</view> -->
						<!-- <view class="bnt gray"></view> -->
						<view class="bnt gray" @click="showCop(item)">详情</view>
					</view>
				</view>
			</view>
		</view>
		<!--暂无优惠券-->
		<view class="noCommodity" v-if="couponsList.length === 0">
			<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" text="暂时没有可用的优惠券">
			</u-empty>
		</view>
	</view>
</template>
<script>
	import {
		list4
	} from "@/request/api/coupon.js"

	const NAME = 'UserCoupon'

	export default {

		data: function() {
			return {
				loading: false,
				show: false,
				content: '',
				couponsList: [],
			}
		},

		async onLoad() {
			let res = await list4();
			this.couponsList = res.data.filter(item => item.status != '2');
		},
		methods: {
			showCop(item) {
				this.content = item.intro;
				this.show = true;
			},
			formatTimestamp(timestamp) {
				// 创建 Date 对象
				const date = new Date(timestamp * 1000); // 将时间戳转换为毫秒
				// 获取年份、月份、日期、小时、分钟、秒
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
				const day = String(date.getDate()).padStart(2, '0');
				const hour = String(date.getHours()).padStart(2, '0');
				const minute = String(date.getMinutes()).padStart(2, '0');
				const second = String(date.getSeconds()).padStart(2, '0');
				// 返回格式化的日期时间字符串
				return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
			},
		},
	}
</script>